{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-07-03T09:27:44.653143Z",
     "start_time": "2024-07-03T09:27:44.636850Z"
    }
   },
   "source": [
    "import string\n",
    "\n",
    "string.ascii_letters\n",
    "string.digits\n",
    "string.printable"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&\\'()*+,-./:;<=>?@[\\\\]^_`{|}~ \\t\\n\\r\\x0b\\x0c'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-01T10:41:33.309792Z",
     "start_time": "2024-07-01T10:41:33.278766Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import sys\n",
    "\n",
    "sys.path"
   ],
   "id": "5c0faff80bafe076",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['F:\\\\PyCharm 2024.1.3\\\\plugins\\\\python\\\\helpers-pro\\\\jupyter_debug',\n",
       " 'F:\\\\PyCharm 2024.1.3\\\\plugins\\\\python\\\\helpers\\\\pydev',\n",
       " 'D:\\\\1.python学习包括基础知识等\\\\basic_knowledge',\n",
       " 'f:\\\\python\\\\python39.zip',\n",
       " 'f:\\\\python\\\\DLLs',\n",
       " 'f:\\\\python\\\\lib',\n",
       " 'f:\\\\python',\n",
       " '',\n",
       " 'f:\\\\python\\\\lib\\\\site-packages',\n",
       " 'f:\\\\python\\\\lib\\\\site-packages\\\\win32',\n",
       " 'f:\\\\python\\\\lib\\\\site-packages\\\\win32\\\\lib',\n",
       " 'f:\\\\python\\\\lib\\\\site-packages\\\\Pythonwin']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-03T09:56:38.275894Z",
     "start_time": "2024-07-03T09:56:38.251134Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 演示typing包\n",
    "# https://docs.python.org/3.9/library/typing.html\n",
    "# 类型暗示：变量申明，函数参数类型，返回值类型\n",
    "\n",
    "a: list[float]\n",
    "a = [1.2, 3.4]\n",
    "print(a)\n",
    "\n",
    "\n",
    "def greeting(name: str) -> str:\n",
    "    return \"Hello, \" + name\n",
    "\n",
    "\n",
    "# 类型别名，有利于简化特殊类型的书写\n",
    "# 下面定义一个Vector类型。使用该类型创建一个向量。最后通过函数scale将向量中的元素缩小或放大一定倍数\n",
    "Vector = list[float]\n",
    "\n",
    "\n",
    "def scale(v: Vector, factor: float) -> Vector:\n",
    "    v = [x * factor for x in v]\n",
    "    return v\n",
    "\n",
    "\n",
    "print(scale([1.23, 3, 45, 5.67], 0.25))\n",
    "\n",
    "# 使用NewType创建新的类型\n",
    "from typing import NewType\n",
    "\n",
    "User = NewType('User', str)\n",
    "user = User('user1')\n",
    "print(user)\n",
    "\n",
    "# 函数参数为函数类型时，指明函数的参数情况和返回值情况\n",
    "from typing import Callable\n",
    "\n",
    "\n",
    "def feeder(get_next_item: Callable[[], str]) -> None:\n",
    "    print(get_next_item())\n",
    "\n",
    "\n",
    "def query() -> str:\n",
    "    return \"hello\"\n",
    "\n",
    "\n",
    "feeder(query)\n",
    "\n",
    "# 泛型定义\n",
    "from typing import TypeVar\n",
    "\n",
    "T = TypeVar('T')\n",
    "\n",
    "\n",
    "def first(l: list[T]) -> T:\n",
    "    return l[0]\n",
    "\n",
    "\n",
    "# Union类型 和 Tuple类型\n",
    "from typing import Union\n",
    "from typing import Tuple\n",
    "from typing import Sequence\n",
    "\n",
    "# 在填写联系方式时，用户可能既选择手机号也有可能是邮箱。其中邮箱分为两部分，以@为界\n",
    "Contact = Tuple[str, int, Union[str, Tuple[str, str]]]\n",
    "contact1 = (\"liu\", 1, \"12345678901\")\n",
    "contact2 = (\"long\", 0, (\"liu\", \"qq.com\"))\n",
    "contact_list: Sequence[Contact] = [contact1, contact2]\n",
    "\n",
    "\n",
    "def get_contact(contact: Contact) -> str:\n",
    "    if contact[1] == 1:\n",
    "        return contact[2]\n",
    "\n",
    "    if contact[1] == 0:\n",
    "        return \"@\".join(contact[2])\n",
    "\n",
    "\n",
    "print(get_contact(contact1))\n",
    "print(get_contact(contact2))\n"
   ],
   "id": "ecbd60e92e67872c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.2, 3.4]\n",
      "[0.3075, 0.75, 11.25, 1.4175]\n",
      "user1\n",
      "hello\n",
      "12345678901\n",
      "liu@qq.com\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-04T01:15:55.195009Z",
     "start_time": "2024-07-04T01:15:55.181796Z"
    }
   },
   "cell_type": "code",
   "source": [
    "f = open('simple_0.simple', 'r')\n",
    "text = f.readline()\n",
    "f.close()\n",
    "print(text[0:5])"
   ],
   "id": "dd980d3260150718",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    i\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-05T03:18:33.923014Z",
     "start_time": "2024-07-05T03:18:33.892370Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from typing import NewType\n",
    "from typing import Tuple\n",
    "from typing import Union\n",
    "from enum import IntEnum\n",
    "\n",
    "\n",
    "token_kind_type = NewType('token_kind_type', str)\n",
    "id_name_type = NewType('id_name_type', str)\n",
    "num_val_type = NewType('num_val_type', int)\n",
    "char_type = NewType('char_type', str)\n",
    "\n",
    "# 返回单词数据结构定义\n",
    "TokenStruct = Tuple[token_kind_type, Union[id_name_type, num_val_type, char_type]]\n",
    "\n",
    "token1=(id_name_type(\"liu\"),token_kind_type(\"id\"))\n",
    "\n",
    "def print_token(token: TokenStruct):\n",
    "    print(token[0],token[1])\n",
    "\n",
    "print_token(token1)\n",
    "\n",
    "\n",
    "token_kinds = (\n",
    "    'add_token', 'becomes', 'bool_token', 'char_token', 'comma', 'else_token',\n",
    "    'end_file', 'eqc_token', 'err_token', 'false_token', 'gtc_token', 'id',\n",
    "    'if_token', 'int_token', 'l_brace', 'l_parent', 'letter', 'ltc_token',\n",
    "    'mul_token', 'num', 'r_brace', 'r_parent', 'read_token', 'return_token',\n",
    "    'semicolon', 'then_token', 'true_token', 'write_token'\n",
    ")\n",
    "\n",
    "TokenKind = IntEnum('TokenKind', token_kinds)\n",
    "list(TokenKind)\n",
    "TokenKind(1)==TokenKind.add_token\n",
    "\n",
    "# 注意源代码中在实现TokenKind的时候使用到了auto函数，同时重写了枚举类Enum里面的_generate_next_value方法，将\n",
    "# 枚举变量的值为字符串类型，此时不能通过TokenKind(1)访问TokenKind.add_token\n"
   ],
   "id": "57f79b02e0e079ba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "liu id\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-07T01:54:06.894541Z",
     "start_time": "2024-07-07T01:54:06.857496Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# python 枚举类型（Enum）\n",
    "from  enum  import Enum,auto,Flag\n",
    "class ColorEnum(Flag):\n",
    "    RED=auto\n",
    "    ORANGE=auto\n",
    "    YELLOW=auto\n",
    "    GREEN=auto\n",
    "    BLUE=auto\n",
    "    \n",
    "    def __repr__(self):\n",
    "        return \"ColorEnum.{}\".format(self.name)\n",
    "    \n",
    "    \n",
    "\n",
    "print(ColorEnum.RED)\n",
    "\n",
    "print(ColorEnum.RED.name,ColorEnum.RED.value) # 获取枚举变量的名称和属性值\n",
    "\n",
    "\n",
    "# 枚举变量的遍历\n",
    "Animal=Enum('Animal',('DOG','CAT','PIG'))\n",
    "animal_values=(animal.value for animal in Animal)\n",
    "print(animal_values)\n",
    "for animal in list(Animal):\n",
    "    print(animal.name,animal.value)\n",
    "\n",
    "Animal(1)==Animal.DOG"
   ],
   "id": "a908a4e893e5a678",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ColorEnum.RED\n",
      "RED <class 'enum.auto'>\n",
      "<generator object <genexpr> at 0x00000252D5FEDCF0>\n",
      "DOG 1\n",
      "CAT 2\n",
      "PIG 3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-08T04:13:47.195647Z",
     "start_time": "2024-07-08T04:13:47.180022Z"
    }
   },
   "cell_type": "code",
   "source": [
    "l=[i**2 for i in range(10)]\n",
    "\n",
    "l.extend([i for i in range(10,20)])\n",
    "l.extend([])\n",
    "l\n",
    "\n",
    "index=1\n",
    "\n",
    "if not index:\n",
    "    print(\"no\")\n",
    "\n",
    "print([].extend([]))\n",
    "print([])\n",
    "print([] is not None)"
   ],
   "id": "e21318cc3ba157cb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "[]\n",
      "True\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "15ba1479bf744a6c"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
